import {ActorState} from "db://assets/Script/Actor/ActorState";
import {StateEnum} from "db://assets/Script/Enums/StateEnum";
import {Vec2, math, v2} from "cc"
import {AnimationEnum} from "db://assets/Script/Enums/AnimationEnum";

export class Run extends ActorState {
    velocity: Vec2 = v2()

    onEnter() {
        console.log("动画状态机进入Run动画")
        this.animation.crossFade(AnimationEnum.PLAYER_RUN)
    }

    onExit() {

    }

    onDestroy() {

    }

    canTransition(to: StateEnum): boolean {
        console.log("Run当前状态准备切换")
        if (to === this.stateEnum) {
            console.log("Run状态无法切换，相同状态")
            return false
        }
        console.log("Run状态切换成功，新状态：", to)
        return true
    }

    update(deltaTime: number) {
        console.log("Run状态更新")
        this.velocity.set(this.actor.input.x,this.actor.input.y)
        this.velocity.multiplyScalar(this.actor.linearSpeed)
        this.actor.rigidBody2D.linearVelocity=this.velocity

        if (this.actor.input.length() <= math.EPSILON) {
            console.log("Run切换Idle状态")
            this.actor.stateManager.transitionTo(StateEnum.IDLE)
        }
    }
}
